<template>
  <el-dialog
    :title="'注册'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm"label-width="80px" @keyup.enter.native="dataFormSubmit()">
      <el-form-item label="邮箱" prop="Email">
        <el-input v-model="dataForm.Email" placeholder="请输入用户名(邮件形式)" label-width="50%"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="Password">
        <el-input type="password" v-model="dataForm.Password" placeholder="请输入6-20位的密码，必须包含数字和字母，支持特殊符号~!@#$%^*"></el-input>
      </el-form-item>
      <el-form-item label="重复密码" prop="RePassword">
        <el-input type="password" v-model="dataForm.RePassword" placeholder="请重复输入密码"></el-input>
      </el-form-item>
      <el-form-item style="margin-bottom: 0">
        <el-button type="primary" style="margin: 2% 0 0 88% " @click="submitReg">注册</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { isEmail } from '@/utils/validate'
export default {
  data () {
    var validatePassword = (rule, value, callback) => {
      if (!this.dataForm.id && !/\S/.test(value)) {
        callback(new Error('密码不能为空'))
      } else if (this.passwordf !== false) {
        callback(new Error('密码格式不正确，请输入6-20位的密码，必须包含数字和字母，支持特殊符号~!@#$%^*'))
      } else {
        callback()
      }
    }
    var validateComfirmPassword = (rule, value, callback) => {
      if (!this.dataForm.id && !/\S/.test(value)) {
        callback(new Error('确认密码不能为空'))
      } else if (this.dataForm.Password !== value) {
        callback(new Error('确认密码与密码输入不一致'))
      } else if (this.passwordf !== false) {
        callback(new Error('密码格式不正确，请输入6-20位的密码，必须包含数字和字母，支持特殊符号~!@#$%^*'))
      } else {
        callback()
      }
    }
    var validateEmail = (rule, value, callback) => {
      if (!isEmail(value)) {
        callback(new Error('邮箱格式错误'))
      } else {
        callback()
      }
    }
    return {
      visible: false,
      passwordf: false,
      dataForm: {
        Email: '',
        Password: '',
        RePassword: ''
      },
      dataRule: {
        Password: [
          {validator: validatePassword, trigger: 'blur'}
        ],
        RePassword: [
          {validator: validateComfirmPassword, trigger: 'blur'}
        ],
        Email: [
          {required: true, message: '邮箱不能为空', trigger: 'blur'},
          {validator: validateEmail, trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
    },
    submitReg () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl('/register'),
            method: 'post',
            data: this.$http.adornData({
              'username': this.dataForm.Email,
              'password': this.dataForm.Password
            })
          }).then(({data}) => {
            console.log(data)
            if (data && data.code === 0) {
              this.$message('注册成功！')
              this.$nextTick(() => {
                this.$http({
                  url: '/sys/login',
                  method: 'post',
                  data: this.$http.adornData({
                    'username': this.dataForm.Email,
                    'password': this.dataForm.Password
                  })
                }).then(({data}) => {
                  if (data && data.code === 0) {
                    this.$cookie.set('token', data.token)
                    this.$router.replace({name: 'home'})
                  } else {
                    this.$message.error(data.msg)
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              })
            } else if (data && data.code === 10001) {
              this.passwordf = true
              this.$refs['dataForm'].validate()
              this.passwordf = false
            }
          })
        }
      })
    }
  }
}
</script>
